<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use think\Request;

/**
 * 诉求发布用接口
 */
class Usersave extends Api
{
    protected $noNeedLogin = [];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];


    //过期诉求
    public function getoutsuqiulist(){

        $userinfo=$this->auth->getUser();

        $lists= Db::name('shenhe')
            ->alias('a')
            ->join('fa_types b','a.types_id = b.id')
            ->where(['a.status'=>'0'])
            ->where(['a.help'=>$userinfo['username']])
            ->field('a.*,b.name as gname')
            ->select();


        foreach ($lists as &$key){

            $key['addtime']=date('Y-m-d H:i',$key['addtime']);

            $key['status_v']=getstatus($key['status']);

            $key['imgs']=getimgs($key['images']);
            $key['nexttime']=date('Y-m-d H:i',$key['nexttime']);

            $user=Db::name('user')->where(['id'=>$key['user_id']])->find();

            $key['user_name']=$user['nickname'];

            $group=Db::name('categorys')->where(['id'=>$user['group_id']])->find();

            $key['user_tip']=$group['name'];


            if($key['nexttime']<strtotime('-1 day')){
                $key['tips']='急需处理';
            }

        }


        $this->success('我的诉求',$lists);


    }
    //需要我处理的诉求
    public function getmysuqiulist(){

        $userinfo=$this->auth->getUser();

        $status=$this->request->param('status');
        $user=Db::name('user')->where(['id'=>$userinfo['id']])->find();
        $cate=Db::name('categorys')->where(['user_id'=>$user['username']])->find();

        if ($cate['level']==4){
            $map['a.p4']=['eq',$cate['user_id']];
        }

        if ($cate['level']==3){
            $map['a.p3']=['eq',$cate['user_id']];
        }

        if ($cate['level']==2){
            $map['a.p2']=['eq',$cate['user_id']];
        }
        if ($cate['level']==1){
            $map['a.p1']=['eq',$cate['user_id']];
        }

        if ($status==0){

            $lists= Db::name('shenhe')
                ->alias('a')
                ->join('fa_types b','a.types_id = b.id')
                ->where(['a.status'=>'0'])
                ->where(['a.level'=>$cate['level']])
                ->where($map)
                ->field('a.*,b.name as gname')
                ->select();

        }

        if($status==1){

            $lists= Db::name('shenhe')
                ->alias('a')
                ->join('fa_types b','a.types_id = b.id')
                ->where('a.level','lt',$cate['level'])
                ->where('a.status','eq','0')
                ->where($map)
                ->field('a.*,b.name as gname')
                ->select();

        }

        if($status==2){
            $lists= Db::name('shenhe')
                ->alias('a')
                ->join('fa_types b','a.types_id = b.id')
                ->where('a.status','neq','0')
                ->where(['a.level'=>$cate['level']])
                ->where($map)
                ->field('a.*,b.name as gname')
                ->select();
        }


        foreach ($lists as &$key){
            $key['addtime']=date('Y-m-d H:i',$key['addtime']);
            $key['status_v']=getstatus($key['status']);
            $key['imgs']=getimgs($key['images']);
            if($key['nexttime']<strtotime('-1 day')){
                $key['tips']='急需处理';
            }
        }
        $this->success('我的诉求',$lists);
    }

    /*
     * 无法处理 提交到上级
     * */
    public function tosuperior(){

        $user=$this->auth->getUser();

        $id=$this->request->param('id');

        $name=$this->request->param('name');

        if (!$name){
            $this->error('请输入反馈结果');
        }

        $info=Db::name('shenhe')->where(['id'=>$id])->find();

        if (!$info){
            $this->error('未找到诉求信息');
        }


        $level=$info['level']-1;

        //下一个处理节点
        Db::name('shenhe')->where(['id'=>$id])
            ->update(['level'=>$level,'nexttime'=>strtotime('+7 day')]);

        $logs['title']=$name;
        $logs['images']=$this->request->param('images');
        $logs['shenhe_id']=$id;
        $logs['addtime']=time();
        $logs['user_id']=$user['id'];

        Db::name('shenhe_log')->data($logs)->insert();

        $this->success('操作成功！');

    }

    /* 完成诉求 */

    public function suqiufulfill(){

        $id=$this->request->param('id');
        $name=$this->request->param('name');
        $image=$this->request->param('imgs');
        $type_name=$this->request->param('type_name');
        $user=$this->auth->getUser();


        if (!$name){
            $this->error('请输入反馈结果');
        }



        $info=Db::name('shenhe')->where(['id'=>$id])->find();

        if (!$info){
            $this->error('未找到诉求信息');
        }

        $updates['status']=1;
        $updates['see']=$type_name=='展示'?'1':'0';

        //下一个处理节点
        Db::name('shenhe')->where(['id'=>$id])->update($updates);


        $logs['title']=$name;
        $logs['images']=$image;
        $logs['shenhe_id']=$id;
        $logs['addtime']=time();
        $logs['user_id']=$user['id'];
        $logs['status']=1;
        Db::name('shenhe_log')->data($logs)->insert();


        $this->success('操作成功！');

    }

        /**
         * 诉求数量
         */

     public function getsuqiunumber(){

            $userinfo=$this->auth->getUser();
            $user=Db::name('user')->where(['id'=>$userinfo['id']])->find();

            $cate=Db::name('categorys')->where(['user_id'=>$userinfo['username']])->find();

            if ($cate['level']==4){
                $mapa['a.p4']=['eq',$cate['user_id']];
            }
            if ($cate['level']==3){
                $mapa['a.p3']=['eq',$cate['user_id']];
            }
            if ($cate['level']==2){
                $mapa['a.p2']=['eq',$cate['user_id']];
            }
            if ($cate['level']==1){
                $mapa['a.p1']=['eq',$cate['user_id']];
            }

            //待处理
            $lists0= Db::name('shenhe')
                ->alias('a')
                ->join('fa_types b','a.types_id = b.id')
                ->where($mapa)
                ->where(['a.status'=>'0'])
                ->where(['a.level'=>$cate['level']])
                ->count();

            //处理中
            $lists1= Db::name('shenhe')
                ->alias('a')
                ->join('fa_types b','a.types_id = b.id')
                ->where($mapa)
                ->where(['a.status'=>'0'])
                ->where('a.level','lt',$cate['level'])
                ->count();

            //已处理
            $lists2= Db::name('shenhe')
                ->alias('a')
                ->join('fa_types b','a.types_id = b.id')
                ->where($mapa)
                ->where('a.status','neq','0')
                ->where(['a.level'=>$cate['level']])
                ->count();



            $this->success('操作成功！',['coun0'=>$lists0,'coun1'=>$lists1,'count2'=>$lists2]);

        }

    /**
     * 提醒用户
     */
    public function sendmsg(){


            $userinfo=$this->auth->getUser();

            $id=$this->request->param('id');

            $suqiu=Db::name('shenhe')->where(['id'=>$id])->find();

            $log['dates']=date('Ymd');
            $log['s_id']=$id;

            $msg=Db::name('msg_log')->where($log)->find();

            if($msg){
                $this->error('请不要重复提交！');
            }

            $us['group_id']=$suqiu['group_id'];
            $us['level']=$suqiu['level'];


            $user=Db::name('user')->where($us)->find();

            $log['user_id']=$user['id'];


            Db::name('msg_log')->data($log)->insert();

            $this->success('操作成功！');

        }


    /**
     * 提醒消息
     */

    public function getmymsg(){
        $userinfo=$this->auth->getUser();
        $lists=Db::name('msg_log')->where(['user_id'=>$userinfo['id']])->order('id desc')->select();
        $this->success('操作成功！',$lists);
    }


    //提交到上级
    public function suqiutosuperior(){

        $user=$this->auth->getUser();
        $id=$this->request->param('id');
        $name=$this->request->param('name');
        $cid=$this->request->param('cid');

        if (!$name){
            $this->error('请输入反馈结果');
        }
        if (!$cid){
            $this->error('请选择转接人');
        }

        $info=Db::name('shenhe')->where(['id'=>$id])->find();

        if (!$info){
            $this->error('未找到诉求信息');
        }

        $cates=Db::name('categorys')->where(['id'=>$cid])->find();

        //下一个处理节点
        Db::name('shenhe')->where(['id'=>$id])
            ->update(['help'=>$cates['user_id'],'level'=>0]);


        $this->success('操作成功！');


    }

    /* 诉求反馈 */

    public function suqiufulfillb(){

        $id=$this->request->param('id');
        $name=$this->request->param('name');
        $image=$this->request->param('imgs');

        $user=$this->auth->getUser();


        if (!$name){
            $this->error('请输入反馈结果');
        }



        $info=Db::name('shenhe')->where(['id'=>$id])->find();

        if (!$info){
            $this->error('未找到诉求信息');
        }

        $updates['status']=0;
        $updates['see']='0';
        $updates['help']='';
        $updates['level']=1;

        //下一个处理节点
        Db::name('shenhe')->where(['id'=>$id])->update($updates);


        $logs['title']=$name;
        $logs['images']=$image;
        $logs['shenhe_id']=$id;
        $logs['addtime']=time();
        $logs['user_id']=$user['id'];
        $logs['status']=2;
        Db::name('shenhe_log')->data($logs)->insert();


        $this->success('操作成功！');

    }

    //fan
    public function getsuqiubyidforback(){

        $id=$this->request->param('id');

        $rets= Db::name('shenhe_log')
            ->where(['status'=>2])
            ->where(['shenhe_id'=>$id])->find();


        if ($rets){
            $rets['ret_img']=getimgs($rets['images']);
        }


        $this->success('诉求反馈！',$rets);

    }

    public function backorder(){
        $id=$this->request->param('id');

        Db::name('shenhe')
            ->where(['id'=>$id])
            ->update(['back'=>1]);


        $this->success('操作成功！');

    }




}


